import inspect
import argparse
import os
import shutil
import sys
sys.path.append('script')
import execute_common
import execute_workload

WORK_DIR = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

def parser():
    arg_parser = argparse.ArgumentParser(description='execute test suite')
    arg_parser.add_argument('--test-suite-kind',
                            '-test-suite-kind',
                            required=True,
                            help=f'kind of the test suite to be executed. For example: workload.')
    arg_parser.add_argument('--test-suite-name',
                            '-test-suite-name',
                            required=True,
                            help=f'Name of the test suite to be executed. For example: original_workload_app, ark_preheat_workload_app, ...')
    arg_parser.add_argument('--code-repository-type',
                            '-code-repository-type',
                            required=True,
                            help=f'Code repository type of arkcompiler. For example: openharmony, openharmony_dev, openharmony_trunk, standalone.')
    arg_parser.add_argument('--local-code-repository-path',
                            '-local-code-repository-path',
                            required=True,
                            help=f'Absolute path of the local code repository directory. For example: /home/linxin/openharmony_dev/verify')
    arg_parser.add_argument('--mode',
                            '-mode',
                            required=True,
                            help=f'Execution mode. For example: gen_abc, x64_interpreter, x64_jit, x64_aot, qemu64_interpreter, qemu64_jit, qemu64_aot, aarch64_interpreter, aarch64_jit, aarch64_aot')
    arg_parser.add_argument('--sn',
                            '-sn',
                            required=False,
                            help=f'Mobile sn, get from hdc list targets. For example: xxxxxxxxxxx')
    arg_parser.add_argument('--ssh-user',
                            '-ssh-user',
                            required=False,
                            help=f'PC user name. When testing in Mode aarch64_xxx, a PC user name is required for ssh connection. For example: linxin')
    arg_parser.add_argument('--ssh-password',
                            '-ssh-password',
                            required=False,
                            help=f'PC password. When testing in Mode aarch64_xxx, a PC password is required for ssh connection. For example: xxxxxx')
    arg_parser.add_argument('--ssh-ip',
                            '-ssh-ip',
                            required=False,
                            help=f'PC ip. When testing in Mode aarch64_xxx, a PC ip is required for ssh connection. For example: 192.168.1.1')
    arg_parser.add_argument('--ssh-port',
                            '-ssh-port',
                            required=False,
                            default=22,
                            help=f'PC ssh server port. When testing in Mode aarch64_xxx, a PC ssh server port is required for ssh connection. default: 22')
    arg_parser.add_argument('--detail-report-excel-tag',
                            '-detail-report-excel-tag',
                            required=False,
                            default='tag',
                            help=f'Report tag. This tag will serve as the title in the Excel report. For examlpe: 2024-08-17')
    arg_parser.add_argument('--repetition-count',
                            '-repetition-count',
                            required=False,
                            default=1,
                            help=f'Repetition count. During the performance test, the number of repetitions is provided to reduce the impact of fluctuation. This parameter takes effect only in aarch64_xxx mode. For examlpe: 10')
    arg_parser.add_argument('--one-case',
                            '-one-case',
                            required=False,
                            default='',
                            help=f'Execute specified cases. The name of the case. For examlpe: wsl')

    args = arg_parser.parse_args()
    return args

def get_test_suite_path(test_suite_name):
    execute_common.TEST_SUITE_OUT_PATH = os.path.join(WORK_DIR, 'out')
    execute_common.TEST_SUITE_SRC_PATH = os.path.join(WORK_DIR, test_suite_name)
    execute_common.TEST_SUITE_ABC_PATH = os.path.join(WORK_DIR, 'out', test_suite_name)
    execute_common.ETS_RUNTIME_AARCH64_LIB_BEFORE = os.path.join(WORK_DIR, 'out/ets_runtime_aarch64_lib_before')
    execute_common.ETS_RUNTIME_AARCH64_LIB_AFTER = os.path.join(WORK_DIR, 'out/ets_runtime_aarch64_lib_after')
    execute_common.HOS_CORE_BAT_PATH = os.path.join(WORK_DIR, 'script')
    execute_common.HOS_SMALL_CORE_MAX_FREQ_BAT = os.path.join(WORK_DIR, 'script/OHSmallCoreMaxFreq.bat')
    execute_common.HOS_ALL_CORE_MAX_FREQ_60 = os.path.join(WORK_DIR, 'script/OHAllCoreMaxFreq-60.bat')
    if not os.path.exists(execute_common.TEST_SUITE_SRC_PATH):
        print("test suite src path is not exist: " + execute_common.TEST_SUITE_SRC_PATH)
        return False
    if os.path.exists(execute_common.TEST_SUITE_ABC_PATH):
        shutil.rmtree(execute_common.TEST_SUITE_ABC_PATH)
    os.makedirs(execute_common.TEST_SUITE_ABC_PATH)
    if os.path.exists(execute_common.ETS_RUNTIME_AARCH64_LIB_AFTER):
        shutil.rmtree(execute_common.ETS_RUNTIME_AARCH64_LIB_AFTER)
    os.makedirs(execute_common.ETS_RUNTIME_AARCH64_LIB_AFTER)
    return True

def get_tool_chain_path(args):
    execute_common.LOCAL_CODE_REPOSITORY_PATH = args.local_code_repository_path
    if args.code_repository_type == 'openharmony':
        execute_common.X64_TOOL_CHAIN_FULL_PATH = args.local_code_repository_path + "/out/rk3568/clang_x64"
        execute_common.AARCH64_TOOL_CHAIN_FULL_PATH = args.local_code_repository_path + "/out/rk3568"
    elif args.code_repository_type == 'openharmony_dev' or args.code_repository_type == 'openharmony_trunk':
        execute_common.X64_TOOL_CHAIN_FULL_PATH = args.local_code_repository_path + "/out/generic_generic_arm_64only/general_all_phone_standard/clang_x64"
        execute_common.AARCH64_TOOL_CHAIN_FULL_PATH = args.local_code_repository_path + "/out/generic_generic_arm_64only/general_all_phone_standard"
    elif args.code_repository_type == 'standalone':
        execute_common.X64_TOOL_CHAIN_FULL_PATH = args.local_code_repository_path + "/out/x64.release"
        execute_common.AARCH64_TOOL_CHAIN_FULL_PATH = args.local_code_repository_path + "/out/arm64.release"
    else:
        print("Unsupported value for tool-chain-path: " + args.local_code_repository_path)
        return False
    return True

def main():
    args = parser()
    if not get_test_suite_path(args.test_suite_name):
        return
    if not get_tool_chain_path(args):
        return
    if args.test_suite_kind == 'workload':
        execute_workload.main(args)

if __name__ == '__main__':
    main()
